package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.entity.ProduceProducts;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 供应商产品生产关联表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-08-23
 */
@Mapper
public interface ProduceProductsMapper extends BaseMapper<ProduceProducts> {

    @Select("SELECT a.*,bb.`name` AS name,c.`name` AS unitName  FROM dragon_produce_products a " +
            "LEFT JOIN dragon_produce aa on a.produce_id = aa.id " +
            "LEFT JOIN dragon_vendor_products bb ON  a.quotes_product_id = bb.id " +
            "LEFT JOIN dragon_dictsort_detail c ON c.code_type = 'unitCode' AND bb.unit = c.id " +
            "  WHERE aa.contract_id = #{contractId}")
    List<ProduceProducts> selectListByContractId(Integer contractId);

    @Select("SELECT a.*,bb.`name` AS name, bb.material as vProductMaterial, ddd.name as unitName, bb.specification as specification, bb.remark AS vProductRemark   FROM dragon_produce_products a " +
            "LEFT JOIN dragon_produce aa on a.produce_id = aa.id " +
            "LEFT JOIN dragon_vendor_products bb ON  a.quotes_product_id = bb.id " +
            "LEFT JOIN dragon_dictsort_detail ddd ON ddd.code_type = 'unitCode' AND bb.unit = ddd.id " +
            "  WHERE a.produce_id = #{produceId}")
    List<ProduceProducts> selectListByProduceId(Integer produceId);

    @Select("<script> " +
                    "SELECT M.id, N.id as quotesProductId, N.name, M.num,M.produce_status,M.scheduling_status, M.is_out, M.scheduled_num, N.unitName,N.specification,N.material as vProductMaterial,N.remark as vProductRemark,M.produce_num,M.deliver_num,M.inventory_num " +
                    "FROM dragon_produce_products M " +
                    "LEFT JOIN " +
                    "(SELECT a.id, a.name, c.name AS unitName,a.specification,a.material, a.remark " +
                    "FROM dragon_vendor_products a " +
                    "LEFT JOIN dragon_dictsort_detail c ON c.code_type = 'unitCode' AND a.unit = c.id) N " +
                    "on M.quotes_product_id = N.id " +
                    "where 1 = 1 " +
                    "<if test='produceId != null and produceId != \"\"'>" +
                    " and M.produce_id = #{produceId}" +
                    "</if>" +
                    "</script>")
    List<ProduceProducts> selectListByProduce(Page<ProduceProducts> productsPage, Integer produceId);

}
